package edu.snake;

import edu.snake.implement.ConfigConstant;

import static edu.snake.implement.ConfigConstant.getInstanceGameEngine;

class Application {

    private Application() {
        getInstanceGameEngine();

    }

    public static void main(String[] args) {
        checkInputParameters(args);
        new Application();
    }

    private static void checkInputParameters(String[] args) {
        int[] argsInt = new int[5];
        StringBuilder error = new StringBuilder();
        if (args != null && args.length > 0) {

            if (args.length != 5) {
                error.append(String.format("Incorrect number of parameters must be five integer." +
                        " You input %d parameter(s). Application not start. \n", args.length));
            }
            int maxConvertParameters = args.length;
            if (args.length > 5) {
                maxConvertParameters = 5;
            }
            for (int i = 0; i < maxConvertParameters; i++) {
                try {
                    argsInt[i] = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    error.append("Parameter: ").append(i).append(" is not Integer. \n");
                }
                if (argsInt[i] < 1) {
                    error.append("Parameter: ").append(i).append(" must be greater than one.\n");
                }
            }
            if (error.length() > 0) {
                throw new IllegalArgumentException(error.toString());
            }


            ConfigConstant.cellWidth = argsInt[0];
            ConfigConstant.cellHeight = argsInt[1];
            ConfigConstant.snakeSize = argsInt[2];
            ConfigConstant.frogCount = argsInt[3];
            ConfigConstant.millisSnake = argsInt[4];

            if (ConfigConstant.cellWidth * ConfigConstant.cellHeight < ConfigConstant.snakeSize + ConfigConstant.frogCount) {
                System.out.println("The size of the arena is too small for placing all the characters.");
                System.exit(-1);
            }

            ConfigConstant.multiplierDelay = 2;
            ConfigConstant.cellSizePixel = 20;


        } else {
            System.out.println("No parameters, the application will use the default values.");
            ConfigConstant.cellWidth = 20;
            ConfigConstant.cellHeight = 20;
            ConfigConstant.snakeSize = 35;
            ConfigConstant.frogCount = 20;
            ConfigConstant.millisSnake = 350;

            ConfigConstant.multiplierDelay = 2;
            ConfigConstant.cellSizePixel = 20;


        }


    }
}
